package it.polimi.travelDream.ejb.servlet;

import it.polimi.travelDream.ejb.interfaces.ExcursionMgrRemote;
import it.polimi.travelDream.ejb.interfaces.PackageMgrRemote;
import it.polimi.travelDream.entities.Package;
import it.polimi.travelDream.entities.Excursion;

import java.io.IOException;
import java.util.Map;
import java.lang.Object;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TravelServlet
 */
@WebServlet("/AddPackageExcursions")
public class AddPackageExcursions extends HttpServlet {
	private static final long serialVersionUID = 1L;


	@EJB
	PackageMgrRemote pmgr;
	
	@EJB
	ExcursionMgrRemote emgr;
	
	public AddPackageExcursions() {
		super();
	}


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {	
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Map<String, String[]> mappa = request.getParameterMap();
		
		System.out.println("entro");
		
		System.out.println("esco");
		for (int i = 0; i < 20; i++){
			System.out.println(i);
			if (mappa.get("idExcursion"+i) != null){
				System.out.println("sono dentro al primo if");
				if (mappa.get("idExcursion"+i)[0] != null){
					System.out.println("sono dentro al secondo if");
					Excursion excursion = new Excursion();
					System.out.println("sono dentro al secondo if prima del mappagetidtransportation");
					String idExcursion = mappa.get("idExcursion"+i)[0];
					System.out.println("sono dentro al secondo if prima di integerparseintidtransportation");
					System.out.println(idExcursion);
					int z = Integer.parseInt(idExcursion);
					System.out.println("Io ho castato..."+z);
					
					excursion = emgr.getExcursion(z);
					System.out.println("sono dentro al secondo if prima di addtransportation");
					pmgr.addExcursion(excursion,Integer.parseInt( mappa.get("idPackage")[0]));
				}
			}
		}
		
		
		request.setAttribute("message","Well done! Package created!");
		
		request.getRequestDispatcher("/BPAdvice.jsp").forward(request, response); 
		//response.getOutputStream().println("<h1>TravelDream</h1><h2>" + mappa.get("name")[0]+ " succesfully registered!</h2>" + "| <a href=\"Index.jsp\"> Login </a> | Registration |");
		
	}

}
